package com.rjjs.Xiaozhi.controller;

import com.rjjs.Xiaozhi.entity.Data;
import com.rjjs.Xiaozhi.entity.FoodPurchase;
import com.rjjs.Xiaozhi.entity.Vo.CommodityWeekFoodPurchaseVo;
import com.rjjs.Xiaozhi.service.FoodPurchaseService;
import dev.langchain4j.agent.tool.P;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.temporal.TemporalAdjusters;

import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

@RestController
@RequestMapping("foodPurchase")
public class FoodPurchaseController {
    @Autowired
    private FoodPurchaseService foodPurchaseService;

    @PostMapping
    public Data getFoodPurchase(String type) {
        List<FoodPurchase> list =   foodPurchaseService.selectFoodPurchase(type);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        String[][] res = new String[list.size()][4];
        int index = 0;
        for (FoodPurchase foodPurchase : list) {
            res[index][0] = foodPurchase.getName();
            res[index][1] =sdf1.format(foodPurchase.getProductionDate());
            res[index][2] =sdf1.format(foodPurchase.getExpireDate());
            res[index][3] = sdf1.format(foodPurchase.getUploadTime());
            index++;
        }
        Data data = new Data();
        data.setData(res);
        return data;
    }

    @PostMapping("count")
    public Data getFoodPurchaseCount(String type) {
        int productionCount = foodPurchaseService.selectFoodPurchaseCount(type,1);
        int expireCount = foodPurchaseService.selectFoodPurchaseCount(type,2);
        int[] ints = {productionCount, expireCount};
        Data data = new Data();
        data.setData(ints);
        return data;
//        return [];
    }

    @PostMapping("CommodityWeek")
    public Data getCommodityWeek(String type) {
        CommodityWeekFoodPurchaseVo commodityWeekFoodPurchaseVo = new CommodityWeekFoodPurchaseVo();
        Map<String,List<Integer>> dataMap = new HashMap<>();
        String[] monthDateRange = getMonthDateRange();
        commodityWeekFoodPurchaseVo.setData(dataMap);
        List<Integer>  footList = foodPurchaseService.getCommodityWeek(type,null,monthDateRange[0],monthDateRange[1]);
        List<Integer>  footJiJianGuoQiList = foodPurchaseService.getCommodityWeek(type,2,monthDateRange[0],monthDateRange[1]);
        Map<String,List<Integer>> map = new HashMap<>();
        map.put("食品总量_",footList);
        map.put("即将过期数量_",footJiJianGuoQiList);
        List<String> xDataList = new ArrayList<>();
        for(int i = 1;i<=5;i++){
            xDataList.add("第" + i + "周");
        }
        commodityWeekFoodPurchaseVo.setData(map);
        commodityWeekFoodPurchaseVo.setxData(xDataList);
        Data data = new Data();
        data.setData(commodityWeekFoodPurchaseVo);
        return data;
    }

    public static String[] getMonthDateRange() {
        LocalDate today = LocalDate.now();
        LocalDate firstDay = today.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate lastDay = today.with(TemporalAdjusters.lastDayOfMonth());
        int daysInMonth = lastDay.getDayOfMonth();
        return new String[]{
                firstDay.toString(),
                lastDay.toString(),
                String.valueOf(daysInMonth)
        };
    }
}
